package com.yanqu.road.dao.impl.activity.flowerstudy;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.flowerstudy.data.cross.CrossFlowerStudyExamSessionData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FlowerStudyCrossExamSessionDaoImpl extends TempDataObjectDao<CrossFlowerStudyExamSessionData> {

    public static String getCrossFlowerStudyExamSessionDataMapSql = "select * from `t_c_flower_study_activity_exam_session` where `activityId`=?";
    public Map<Integer,CrossFlowerStudyExamSessionData> integerCrossFlowerStudyExamSessionDataMap(int activityId) {
        Map<Integer,CrossFlowerStudyExamSessionData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFlowerStudyExamSessionDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossFlowerStudyExamSessionData data = parse(rs);
                map.put(data.getSessionId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getCrossFlowerStudyExamSessionDataSql = "select * from `t_c_flower_study_activity_exam_session` where `activityId`=? and `sessionId`=?";
    public CrossFlowerStudyExamSessionData getCrossFlowerStudyExamSessionData(int activityId,int sessionId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFlowerStudyExamSessionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, sessionId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossFlowerStudyExamSessionDataSql = "update `t_c_flower_study_activity_exam_session` set roomType=?,state=?,roomId=? where activityId=? and sessionId=?";
    @Override
    protected boolean updateData(CrossFlowerStudyExamSessionData crossFlowerStudyExamSessionData) {
        crossFlowerStudyExamSessionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getRoomType()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getState()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getRoomId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getSessionId()));
        if (getDb().executeNoneQuery(updateCrossFlowerStudyExamSessionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossFlowerStudyExamSessionDataSql = "insert into `t_c_flower_study_activity_exam_session`(activityId,sessionId,roomType,state,roomId) values (?,?,?,?,?)";
    @Override
    protected boolean addData(CrossFlowerStudyExamSessionData crossFlowerStudyExamSessionData) {
        crossFlowerStudyExamSessionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getSessionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getRoomType()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getState()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamSessionData.getRoomId()));
        if (getDb().executeNoneQuery(insertCrossFlowerStudyExamSessionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossFlowerStudyExamSessionData parseData(ResultSet rs) throws SQLException {
        CrossFlowerStudyExamSessionData crossFlowerStudyExamSessionData = new CrossFlowerStudyExamSessionData();
        crossFlowerStudyExamSessionData.setActivityId(rs.getInt("activityId"));
        crossFlowerStudyExamSessionData.setSessionId(rs.getInt("sessionId"));
        crossFlowerStudyExamSessionData.setRoomType(rs.getInt("roomType"));
        crossFlowerStudyExamSessionData.setState(rs.getInt("state"));
        crossFlowerStudyExamSessionData.setRoomId(rs.getInt("roomId"));

        return crossFlowerStudyExamSessionData;
    }
}
